package com.maxsalad.uc.service;

import com.maxsalad.common.message.MessageResult;
import com.maxsalad.uc.dao.ApiParameterDao;
import com.maxsalad.uc.dao.PlatformShopDao;
import com.maxsalad.uc.pojo.TPlatformShopEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-16.
 */
@Service
public class PlatformShopService extends BaseService {
    private static final Logger log = LoggerFactory.getLogger(PlatformShopService.class);

    @Autowired
    private PlatformShopDao platformShopDao;
    @Autowired
    private ApiParameterDao apiParameterDao;

    public Map queryAllPlatformShop(TPlatformShopEntity platformShopEntity) throws Exception {
        return platformShopDao.queryAllPlatformShop(platformShopEntity);
    }


    public MessageResult saveOrUpdatePlatformShop(TPlatformShopEntity platformShopEntity) throws Exception {
        if (platformShopEntity.getPlatformId() <= 0L) {
            return getMessageResult("E00006");
        }
        if (platformShopEntity.getShopId() <= 0L) {
            return getMessageResult("E00007");
        }
        if (StringUtils.isBlank(platformShopEntity.getShopPid())) {
            return getMessageResult("E00008");
        }
        if (StringUtils.isBlank(platformShopEntity.getBaseUrl())) {
            return getMessageResult("E00009");
        }
        if (StringUtils.isBlank(platformShopEntity.getAppKey())) {
            return getMessageResult("E00010");
        }
        if (StringUtils.isBlank(platformShopEntity.getAppSecret())) {
            return getMessageResult("E00011");
        }

        if (false == platformShopEntity.getBaseUrl().endsWith("/")) {
            platformShopEntity.setBaseUrl(platformShopEntity.getBaseUrl() + "/");
        }

        platformShopDao.saveOrUpdate(platformShopEntity);
        apiParameterDao.refresh();

        return getMessageResult("S00001");
    }

    public MessageResult deletePlatform(long id) throws Exception {
        platformShopDao.delete(id);
        apiParameterDao.refresh();

        return getMessageResult("S00001");
    }
}
